package com.igeekhome.util;

import com.igeekhome.service.IFileUploadService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author 立以宁
 */


public class CommonUtil {
	private CommonUtil() {}

	/**
	 * 如果是空，返回值为真
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if (str == null || str.length() == 0) {
			return true;
		}
		return false;
	}





	/**
	 * 为每个文件生成唯一的名字
	 * @param fileOriginalName
	 * @return
	 */
	public static String getUniqueFileName(String fileOriginalName) {
		//获取文件后缀
		String fileSuffix = fileOriginalName.substring(fileOriginalName.lastIndexOf(".")+1);
		//生成文件唯一名
		String UniqueFileName = UUID.randomUUID().toString().replace("-", "");

		String fileName = UniqueFileName +"."+ fileSuffix;
		return fileName;
	}


	/**
	 *
	 * @param file
	 * @param path 文件存放路径
	 * @param fileOriginalName 原始文件名
	 * @return 返回相对路径
	 */
	public static String uploadImage(MultipartFile file,String path,String relativePath,String fileOriginalName) {


		String newFileName = CommonUtil.getUniqueFileName(fileOriginalName);
		String newPath = path + "\\" + newFileName;
		System.out.println("newPath: "+newPath);
		File dest = new File(newPath);

		//判断文件父目录是否存在
		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdir();
		}

		//虚拟路径拼接
		String url = relativePath +"/"+ newFileName;
		System.out.println("url: "+url);
		try {
			file.transferTo(dest);
			return url;
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;

	}

	public static boolean isAnyEmpty(String... strs) {
		if (strs != null && strs.length > 0) {
			for (String str : strs) {
				if (str == null || str.length() == 0) {
					return true;
				}
			}
		}
		return false;
	}
	

	public static boolean isManyEmpty(String... strs) {
		if (strs != null && strs.length > 0) {
			for (String str : strs) {
				if (str != null && str.length() != 0) {
					return false;
				}
			}
		}
		return true;
	}
	
	public static Date getDate(String parttern,String dateStr) {
		if (isAnyEmpty(parttern,dateStr)) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(parttern);
		
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	public static String toDate(String parttern,Date date) {
		if (isAnyEmpty(parttern)||date == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(parttern);
		return sdf.format(date);
	}

	/**
	 * 可能会使用到的拦截器的放行规则
	 */
	public static boolean releaseSuffix(HttpServletRequest req) {
		return 	req.getRequestURI().contains(".js")
				||req.getRequestURI().contains(".css")||req.getRequestURI().contains(".png")
				||req.getRequestURI().contains(".jpg")||req.getRequestURI().contains(".ico")
				||req.getRequestURI().contains(".json")||req.getRequestURI().contains(".woff")
				||req.getRequestURI().contains(".svg")||req.getRequestURI().contains(".md")
				||req.getRequestURI().contains(".map")||req.getRequestURI().contains(".txt")
				||req.getRequestURI().contains(".gif")||req.getRequestURI().contains(".tff")
				||req.getRequestURI().contains(".eot")||req.getRequestURI().contains(".properties")
				||req.getRequestURI().contains(".xml")||req.getRequestURI().contains(".yaml")
				||req.getRequestURI().contains(".yml")||req.getRequestURI().contains(".md");
	}

	/**
	 * @author:wx
	 * @Description: 密码明文MD5加密
	 * @Param [str]密码明文
	 * @return java.lang.String
	 **/
	public static String md5(String str) {
		MessageDigest instance = null;
		try {
			instance = MessageDigest.getInstance("MD5");
			
			instance.update(str.getBytes());
			
			return new BigInteger(1, instance.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}
	


	

	public static String getAddressId() {
		String AddressId = "ADDRESS";
		Integer random = (int)((Math.random()*9+1)*10000);
		AddressId += random.toString();
		return AddressId;
	}


	/**
	 * 详细地址返回前台数组
	 * @param address 数据库地址
	 * @return 详细地址数组
	 */
	public static String[] addressToList(String address){
		return address.split(",");
	}



	/**
	 * 省市区详细地址字符串拼接
	 * @param province 省
	 * @param city 城市
	 * @param area 区域
	 * @param address 详细地址
	 * @return 字符串
	 */
	public static String toSplicing(String province,String city,String area,String address){
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(province);
		stringBuilder.append(",");
		stringBuilder.append(city);
		stringBuilder.append(",");
		stringBuilder.append(area);
		stringBuilder.append(",");
		stringBuilder.append(address);
		String address_id = stringBuilder.toString();
		return address_id;
	}


}
